package com.sgcc.epri.dcloud.controller;

import com.sgcc.epri.dcloud.pojo.Result;
import com.sgcc.epri.dcloud.service.ShowTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

@RestController
@RequestMapping("/showTable")
@Api(description = "统计表格展示及导出")
public class ShowTableController {

    @Autowired
    private ShowTableService showTableService;

    @GetMapping("test")
    @ApiOperation("刚进入页面时什么都不选，右侧展示文字[运行数据治理报告]")
    public void test(){

    }

    @GetMapping("queryTableByTreeId")
    @ApiOperation("根据treeId查询表格")
    public Result queryTableByTreeId(@RequestParam(value = "treeId",required = true)@ApiParam(value = "树形结果id ",required = true) String treeId,
                                     @RequestParam(value = "dataType",required = false)@ApiParam(value = "数据类型, 此条件由此接口返回的数据决定是否展示",required = false) String dataType,
                                     @RequestParam(value = "objectIds",required = false)@ApiParam(value = "对象id：all（全部）、若选择请用逗号将id隔开传递",required = false) String objectIds,
                                     @RequestParam(value = "showType",required = false)@ApiParam(value = "数值种类 0:得分、1:数值",required = false) String showType,
                                     @RequestParam(value = "statisticsType",required = false)@ApiParam(value = "统计维度 0:单天、1:总数 此条件由此接口返回的数据决定是否展示",required = false) String statisticsType){
        return showTableService.queryTableByTreeId(treeId, dataType, objectIds, showType, statisticsType);
    }

    @GetMapping("excelTable")
    @ApiOperation("导出报表")
    public void excelTable(@RequestParam(value = "treeId",required = true)@ApiParam(value = "树形结构id ",required = true) String treeId,
                           @RequestParam(value = "dataType",required = false)@ApiParam(value = "数据类型, 此条件由此接口返回的数据决定是否展示",required = false) String dataType,
                           @RequestParam(value = "objectIds",required = false)@ApiParam(value = "对象id：all（全部）、若选择请用逗号将id隔开传递",required = false) String objectIds,
                           @RequestParam(value = "showType",required = false)@ApiParam(value = "数值种类 0:得分、1:数值",required = false) String showType,
                           @RequestParam(value = "statisticsType",required = false)@ApiParam(value = "统计维度 0:单天、1:总数 此条件由此接口返回的数据决定是否展示",required = false) String statisticsType,
                           HttpServletRequest request, HttpServletResponse response){
        showTableService.excelTable(treeId, dataType, objectIds, showType, statisticsType, request, response);
    }

    @GetMapping("particulars")
    @ApiOperation("数据详情 滚动翻页")
    public Result particulars(@RequestParam(value = "treeId",required = true)@ApiParam(value = "树形结构id ",required = true) String treeId,
                              @RequestParam(value = "datasource",required = true)@ApiParam(value = "数据源id ",required = true) String datasource,
                              @RequestParam(value = "datasourceName",required = false)@ApiParam(value = "数据源名称 访问二级详情时使用",required = false) String datasourceName,
                              @RequestParam(value = "tableHeadId",required = false)@ApiParam(value = "表头id ",required = false) String tableHeadId,
                              @RequestParam(value = "indexId",required = true)@ApiParam(value = "指标id ",required = true) String indexId,
                              @RequestParam(value = "objectId",required = false)@ApiParam(value = "对象id ",required = false) String objectId,
                              @RequestParam(value = "dataType",required = false)@ApiParam(value = "数据类型 0电量、1非电量",required = false) String dataType,
                              @RequestParam(value = "dateType",required = false)@ApiParam(value = "数据时间类型 0:单天、1:总数  如果当前指标是1003，那该参数是必填的",required = false) String dateType,
                              @RequestParam(value = "contentType",required = true)@ApiParam(value = "详情级别 101表示一级，102表示二级",required = true) String contentType,
                              @RequestParam(value = "assetId",required = false)@ApiParam(value = "资产id，访问二级详情时使用",required = false) String assetId,
                              @RequestParam(value = "assessmentInterval",required = false)@ApiParam(value = "考核区间，访问准确率二级详情时使用,开始和结束时间用逗号拼接作为参数",required = false) String assessmentInterval,
                              @RequestParam(value = "showType",required = true)@ApiParam(value = "数值种类  0:得分、1:数值 ",required = true) String showType,
                              @RequestParam(value = "dayTime",required = false)@ApiParam(value = "当表格按天按展示时，表头作为dayTime传入",required = false) String dayTime,
                              @RequestParam(value = "page",required = true)@ApiParam(value = "页数 ", name = "page", required = true, defaultValue = "1") Integer page,
                              @RequestParam(value = "limit",required = true)@ApiParam(value = "条数 ", name = "limit", required = true, defaultValue = "10") Integer limit){
        return showTableService.particulars(treeId, datasource, indexId, objectId, dataType, dateType, contentType, page, limit, showType, tableHeadId, assetId, datasourceName, dayTime, assessmentInterval);
    }

    @GetMapping("generateReports")
    @ApiOperation("生成报告按钮只在完整率指标相关的报表中展示")
    public void generateReports(@RequestParam(value = "treeId",required = true)@ApiParam(value = "树形结构id ",required = true) String treeId,
                                HttpServletRequest request, HttpServletResponse response){
        showTableService.generateReports(treeId, request, response);
    }


}
